/**
 * Created by kevin on 2017/07/08.
 * Main.js
 * 基类
 * 功能：
 * 1，加载资源
 * 2，实例模块
 * 共分为5层
 * level1 背景层 bgLayer
 * level2 页面内容层 contentLayer
 * level3 公共UI层 UILayer
 * level4 弹窗层 popupLayer
 */
var loaderPage;
var VideoPage;//视频loader
var bgLayer,contentLayer,UILayer,popupLayer,loadingLayer;
var Assembly;//浮层
var GLOBAL_RATIO = 1;//全局缩放比例
var IS_INITIAL = false;//是否已经初始化
var HAS_STAGE = false;
var LAND_PAGE = null;//入口页面
var All_length=[];//通过长度记录加载了多少个页面;
var preload_start_time=0;
window.BaseLoader_list;
/*
*
* */
function prepare() {

    var oriention = hengshuping();
    if(!MY_SCREEN_ORIENTION){
        alert("请设置屏幕横竖!");
        return;
    }
    if(oriention != MY_SCREEN_ORIENTION){
        //alert("AAAAA")
        return;
    }

    //html canvas 元素
    canvas = document.getElementById("canvas");
    if(canvas == undefined || canvas == null){
        alert("No canvas or id is not match!");
        return;
    }
    targetWidth = getWidth();
    targetHeight = getHeight();
    canvas.width = targetWidth;
    canvas.height = targetHeight;

    //var scale = .5;
    canvas.style.transform="scale("+.5+","+.5+")";
    canvas.style.top=-(targetHeight)/4+"px";
    canvas.style.left=-(targetWidth)/4+"px";

    // loadCoreRes();
    createloadCoreRes();
    IS_INITIAL = true;
}
/*
* showhide oriention mask
* */
function showHideOrentionMask(screen_oriention){
    if(screen_oriention == SCREEN_ORIENTION_V){
        console.log("show SCREEN_ORIENTION_V");
    }else{
        console.log("show SCREEN_ORIENTION_H");
    }
}

/*
* loader核心资源加载
* */
function createloadCoreRes() {
    var Mainloader = new createjs.LoadQueue(true);
    //coreload.on("fileload", handleFileLoaded);
    Mainloader.on("complete",drawLoadPage , this);
    Mainloader.loadManifest({src: "./static/MainManifest.json?v="+version, callback: "loadMainMediaGrid", type: "manifest"}, true, "./");
}
function drawLoadPage(){
    initStage();
    // if(!Video.html_play_src){
        createLoading();
    // }
    // createloadVideoRes();
    if(!Video.USE_VIDEO){
        loadCoreRes();
    }else{
        createVideo();
    }
}

function createVideo(){
    VideoPage = new eosi.website.VideoPlayer();
}

/*
* createLoading资源加载
* */
function createLoading(){
    if(loading_page.open){
        loaderPage = new eosi.website.Loading();
        loadingLayer.addChild(loaderPage);
    }
}
/*
* 核心资源加载
* */
function loadCoreRes() {
    coreloader = new createjs.LoadQueue(true);
    //coreload.on("fileload", handleFileLoaded);
    coreloader.on("complete", loaderComplete, this);
    coreloader.on("progress", loaderOnProgress, this);
    coreloader.loadManifest({src: "./static/CoreManifest.json?v="+version, callback: "loadCoreMediaGrid", type: "manifest"}, true, "./assets/");
}

function loaderComplete(evt){
    if(!HAS_STAGE){
        initStageChild();
    }
    preload_start_time=(new Date()).valueOf();
    //onComplete(evt);

}

function loaderOnProgress(evt){
    //console.log(Math.floor(evt.progress*100));
    onProgress(evt,Video_RES_SIZE_PER,CORE_RES_SIZE_PER);//loadingDefault.js

}

/*
* 初始化舞台StageChild
* */
function initStageChild() {
    // this.stage = new createjs.Stage(canvas);
    FastClick.attach(document.body);
    createjs.Touch.enable(this.stage);

    bgLayer = new eosi.website.BgLayer();
    contentLayer = new eosi.website.ContentLayer();
    UILayer = new eosi.website.UILayer();
    popupLayer = new eosi.website.PopupLayer();
    Assembly= new eosi.website.Assembly();
    // loadingLayer = new eosi.website.BaseLoading();

    ver_txt = new createjs.Text(version, "12px Arial", "#555")
    ver_txt.y = targetHeight - ver_txt.getMeasuredHeight();
    this.stage.addChild(bgLayer, contentLayer ,UILayer ,popupLayer,ver_txt);
    // this.stage.setChildIndex(loadingLayer,8);
    // console.log(this.stage.getChildByName("loadingLayer"));
    this.stage.setChildIndex(loadingLayer,5);
    HAS_STAGE = true;

}

/*
* 初始化舞台
* */
function initStage() {
    this.stage = new createjs.Stage(canvas);
    createjs.Touch.enable(this.stage);
    loadingLayer = new eosi.website.BaseLoading();
    // loadingLayer.name="loadingLayer";
    this.stage.addChild(loadingLayer);
    // console.log(this.stage.getChildByName("loadingLayer"));
    // this.stage.setChildIndex(this.stage.getChildByName("loadingLayer"),8);
    createjs.Ticker.addEventListener("tick", tick);

}



function tick(evt){
    //console.log(evt);
    if(UILayer){
        UILayer.tick();
    }
    if(contentLayer){
        contentLayer.tick();
    }
    stage.update(evt);

}
//手势
var blue      = document.getElementById('canvas');
var hammer    = new Hammer.Manager(blue);
var swipe     = new Hammer.Swipe();

hammer.add(swipe);

hammer.on('swipeup', function(){
    //console.log("swipeup");
    if(eosi.website.Scroller.SCROLLER){
        eosi.website.Scroller.SCROLLER.swipe("swipeup");
    }
    //console.log(eosi.website.Scroller.SCROLLER);
});

hammer.on('swipedown', function(){
    //console.log("swipedown");
    if(eosi.website.Scroller.SCROLLER){
        eosi.website.Scroller.SCROLLER.swipe("swipedown");
    }
});
